package com.shun.gen.service;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.Collections;

public class GenJavaCodeService {

    //URL
    private static final String URL = "jdbc:mysql://172.21.11.15:3306/gjyddb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";

    //连接数据库的用户名
    private static final String USRENAME = "root";

    //连接数据库的密码
    private static final String PASSWORD = "root";

    //MAPPER映射的文件路径
    private static final String MAPPER_XML_DIR = "D:\\tmp\\shun\\shun-gjyd-media\\src\\main\\resources\\mapper";

    //代码映射的文件路径
    private static final String JAVA_DIR = "D:\\tmp\\shun\\shun-gjyd-media\\src\\main\\java";

    /**
     * 生成代码
     */
    public static void genCode(String tableName,String moduleName) {
        FastAutoGenerator
                .create(URL, USRENAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("clever") // 设置作者
                            .disableOpenDir()
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(JAVA_DIR); // 指定输出目录
                }).packageConfig(builder -> {
            builder.parent("com.shun") // 设置父包名
                    .entity("vo")
                    .controller("action")
                    .moduleName(moduleName) // 设置父包模块名
                    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, MAPPER_XML_DIR)); // 设置mapperXml生成路径
        }).strategyConfig(builder -> {
            builder.addInclude(tableName).controllerBuilder().formatFileName("%sAction"); // 设置过滤表前缀
        }).execute();
    }
}
